
#include <gtest/gtest.h>
#include <filesystem>
#include <gdstk/gdstk.hpp>
#include <iostream>
#include "qlib/all.h"

namespace fs = std::filesystem;

using namespace qlib;

TEST(GdstkReadOasTest, BasicAssertion) {
  // std::string fileName = "E:/users/xqf/qlayout-test/data/oas/test.OAS";
  //   std::string fileName = "./first.oas";
  //   gdstk::Set<gdstk::Tag> shape_tags;
  //   gdstk::ErrorCode err = gdstk::ErrorCode::NoError;
  //   gdstk::Library library = gdstk::read_oas(fileName.c_str(), 0.001, 0.001, &err);
  //   std::cout << (int)(err) << std::endl;
  EXPECT_EQ(1, 1);
}

TEST(ReadOasTest, BasicAssertion) {
  // std::string fileName = "E:/users/xqf/qlayout-test/data/oas/test.oas";
  std::string fileName = "./first.oas";
  std::set<Tag> shape_tags;
  ErrorCode err = ErrorCode::NoError;
  Library::s_ptr first_library = DB::read_oas(fileName.c_str(), 0, 0, &err);
  EXPECT_EQ(first_library->cells.size(), 2);
  EXPECT_EQ(first_library->cells[0]->properties.size(), 4);

  // path测试
  auto path_wg = Path::create();
  path_wg->init_waveguide(Vec2dArray{Vec2d{0, 0}, Vec2d{100, 200}}, 5, 10, 5, 1e-2, 1, 2);
  auto path_spine = Path::create();
  path_spine->init_spine(Vec2dArray{Vec2d{0, 0}, Vec2d{300, 400}});

  auto text = Text::create();
  text->init("Hello", 1, Vec2d{0, 0}, true, 2, 3);

  first_library->cells[0]->add(path_wg);
  first_library->cells[0]->add(path_spine);
  first_library->cells[0]->add(text);

  err = DB::write_oas(first_library, "test.oas");
  std::cout << (int)(err) << std::endl;

  Library::s_ptr test_library = DB::read_oas("test.oas", 0, 0, &err);
  EXPECT_EQ(test_library->cells.size(), 2);
  EXPECT_EQ(test_library->cells[0]->properties.size(), 4);

  EXPECT_EQ(1, 1);
}

std::string removeExtension(const std::string& filename) {
  size_t lastDot = filename.find_last_of('.');
  if (lastDot == std::string::npos)
    return filename;
  return filename.substr(0, lastDot);
}

TEST(GdsToOasTest, BasicAssertion) {
  std::string directoryPath = "./data";
  if (!fs::exists(directoryPath)) {
    fs::create_directory(directoryPath);
    std::cout << "Created directory: " << directoryPath << std::endl;
  } else {
    std::cout << "Directory already exists: " << directoryPath << std::endl;
  }
  // std::string fileName = "E:/users/xqf/qlayout-test/data/oas/test.oas";
  for (const auto& entry : fs::directory_iterator("../../../data/gds/")) {
    // std::cout << entry.path() << std::endl;
    if (fs::is_directory(entry.path())) {
      std::cout << entry.path().string() << " is a directory." << std::endl;
    } else {
      std::cout << entry.path().string() << " is a regular file." << std::endl;
      auto file_path = entry.path().string();
      auto file_name = entry.path().filename().string();
      // std::string fileName = "../../../data/gds/138bit_line12.gds";
      qlib::ErrorCode err = qlib::ErrorCode::NoError;
      auto lib = qlib::DB::read_gds(file_path.c_str(), 1e-6, 0.01, &err);

      std::string noExtensionName = removeExtension(file_name);
      std::string oas_filename = "./data/" + noExtensionName + ".oas";
      err = qlib::DB::write_oas(lib, oas_filename);
    }
  }

  //   std::cout << (int)(err) << std::endl;

  //   Library::s_ptr test_library = DB::read_oas("test.oas", 0, 0, &err);

  EXPECT_EQ(1, 1);
}